package com.example.administrator.shzs.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.utils.ToastUtils;
import com.example.administrator.shzs.activity.BaseActivity;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.url.Contents;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 编辑地址
 */
public class CompileAddressActivity extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_save_address_compile_activity)
    TextView tvSaveAddress;
    @Bind(R.id.et_shipping_name)
    EditText etShippingName;
    @Bind(R.id.et_shipping_phone)
    EditText etShippingPhone;
    @Bind(R.id.et_detail_address)
    EditText etDetailAddress;

    private Context ctx;

    private String dataId;
    private double longitude;
    private double latitude;
    private String userid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_compile);
        ButterKnife.bind(this);
        MyApplication.app.activityList.add(this);
        ctx=this;
        getAddressData();

        tvSaveAddress.setOnClickListener(this);
        ivBack.setOnClickListener(this);
    }




    private void getAddressData() {

        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String address = intent.getStringExtra("address");
        String phone = intent.getStringExtra("phone");
        dataId = intent.getStringExtra("dataId");
        userid=intent.getStringExtra("userid");
        longitude = intent.getDoubleExtra("longitude", 0);
        latitude = intent.getDoubleExtra("latitude", 0);

        etShippingName.setText(name);
        etShippingPhone.setText(phone);
        etDetailAddress.setText(address);
    }

    @Override
    public void onClick(View v) {

        String name=etShippingName.getText().toString().trim();
        String phone=etShippingPhone.getText().toString().trim();
        String address=etDetailAddress.getText().toString().trim();

        switch (v.getId()){

            case R.id.iv_back:

                finish();
                break;

            case R.id.tv_save_address_compile_activity:

                if(!name.equals("") || !phone.equals("") || !address.equals("")) {
                    //保存地址
                    compileAddress(name,phone,address);
                }else if(name.equals("")){
                    ToastUtils.showMessageShort(this,"请输入收货人姓名");
                }else if(phone.equals("")){
                    ToastUtils.showMessageShort(this,"请输入收货人电话号码");
                }else if(address.equals("")){
                    ToastUtils.showMessageShort(this,"请输入收货人详细地址");
                }


                break;
        }
    }

    private void compileAddress(String name, String phone, String address) {

        String url= Contents.USER_NEW_ADD_USER_SHIPPING_ADDRESS;

        RequestParams params=new RequestParams(url);
        params.addBodyParameter("receiver",name);
        params.addBodyParameter("address",address);
        params.addBodyParameter("mobilephone",phone);
        params.addBodyParameter("userid",userid);
        params.addBodyParameter("dataid",dataId);
        params.addBodyParameter("lng",longitude+"");
        params.addBodyParameter("lat",latitude+"");
        params.addBodyParameter("op","0");

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Log.i("","*****************编辑地址"+result);

                try {

                    JSONObject object =new JSONObject(result);
                    int state = object.getInt("state");
                    if(state==1){
                        ToastUtils.showMessageShort(ctx,"修改成功");
                        setResult(RESULT_OK);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });

    }
}
